package org.flexlancer.databases
{
	public class EnvironmentVariables
	{
		//-------- DEFINED VARIABLES --------------------------
		public static const INIT_VEHICLE_TREE:String = "INIT_VEHICLE_TREE";
		
		//-----------------------------------------------------
		private static const TABLE:String  = "environmentvariables";
		
		private static function List(condition:String = ""):Array{
			try{
				return DatabaseManager.Manager.Select(TABLE,null,condition);
			}catch(err:Error){
				throw err;
			}
			return new Array();
		}
		
		private static function Add(parmName:String, parmValue:String):void{
			try{
				var insert:Object = new Object();
				insert.name = " '"+parmName+"' ";
				insert.value = " '"+parmValue+"' ";
				DatabaseManager.Manager.Insert(TABLE,insert);
			}catch(err:Error){
				throw err;
			}
		}
		
		private static function Update(parmName:String,parmValue:String):void{
			try{
				var update:Object = new Object();
				update.name = " '"+parmName+"' ";
				update.value = " '"+parmValue+"' ";
				DatabaseManager.Manager.Update(TABLE,update,"name LIKE "+update.name);
			}catch(err:Error){
				throw err;
			}
		}
		
		private static function Delete(condition:String = ""):void{
			try{
				DatabaseManager.Manager.Delete(TABLE,condition);
			}catch(err:Error){
				throw err;
			}
		}
		
		private static function GetVariable(parmName:String):String{
			try{
				var arr:Array = List("name LIKE '"+parmName+"'");
				if ( arr.length > 0 ){
					return arr[0].value;
				}else{
					return "";
				}
			}catch(err:Error){
				throw err;
			}
			return "";
		}
		
		private static function SetVariable(parmName:String,parmValue:String):void{
			try{
				if ( List("name LIKE '"+parmName+"'").length == 0 ){
					Add(parmName,parmValue);
				}else{
					Update(parmName,parmValue);
				}
			}catch(err:Error){
				throw err;
			}
		}
//----------------------------------------------------------------------------------------		
//----------------------------------------------------------------------------------------		
//----------------------------------------------------------------------------------------		
//					PUBLIC STATIC FUNCTIONS
//----------------------------------------------------------------------------------------		
//----------------------------------------------------------------------------------------		
//----------------------------------------------------------------------------------------		
		public static function Inits():void{
			try{
				if ( EnvironmentVariables.InitVehicle != "1" ){
					VehicleTree.Init();
					EnvironmentVariables.InitVehicle = "1";
				}
			}catch(err:Error){
				throw err;
			}
		}
		
		public static function set InitVehicle(val:String):void{
			try{
				SetVariable(INIT_VEHICLE_TREE,val);
			}catch(err:Error){
				throw err;
			}
		}
		
		public static function get InitVehicle():String{
			try{
				return GetVariable(INIT_VEHICLE_TREE);
			}catch(err:Error){
				throw err;
			}
			return "";
		}
	}
}